<?php declare(strict_types=1);
/**
 * @author      guosir
 * @copyright   2023-2025 owner
 **/

namespace Srv\Apps\Apis\Skit;

use Srv\Apps\Apis\Log\Ctl\LogCtl;
use Srv\Apps\Apis\Skit\Ctl\SkitChasingCtl;
use Srv\Apps\Proto\DTO\Skit\SkitChasingAddDTO;
use Srv\Apps\Proto\DTO\Skit\SkitChasingDelDTO;
use Srv\Apps\Proto\DTO\Skit\SkitChasingEditDTO;
use Srv\Apps\Proto\DTO\Skit\SkitChasingListDTO;
use Srv\Apps\Proto\Enum\ErrCodeEnum;
use Srv\Apps\Proto\Enum\LangKeyEnum;
use Srv\Apps\Proto\Enum\Log\LogResultEnum;
use Srv\Apps\Proto\VO\Skit\SkitChasingListVO;
use Srv\Libs\ConfModel\RouteConfData;
use Srv\Libs\Frame\ActionAbstract;
use Srv\Libs\Frame\DTOAbstract;
use Srv\Libs\Frame\Lang;
use Srv\Libs\Frame\Page;
use Srv\Libs\Frame\ResponseData;
use Srv\Libs\Frame\VOAbstract;

final class SkitChasingAct extends ActionAbstract
{
    /**
     * @url /skit/chasing/list
     * @auth skitChasingList
     * @req Srv.Apps.Proto.DTO.Skit.SkitChasingListDTO
     * @rsp Srv.Apps.Proto.VO.Skit.SkitChasingListVO
     * 追剧记录默认配置列表
     */
    public function getSkitChasingList(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        if(!($DTOClass instanceof SkitChasingListDTO) || !($VOClass instanceof SkitChasingListVO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $Page                   = new Page();
        $currPage               = max(1, $DTOClass->getCurrPage());
        $pageSize               = $DTOClass->getPageSize() < 1 ? 12 : $DTOClass->getPageSize();
        $Page->init(0, $pageSize, $currPage);
        $whereData              = $RouteConfData->getParamsResData($DTOClass);
        $VOClass                = SkitChasingCtl::tag()->getSkitChasingList($Page, $whereData);
        return ResponseData::rspSuccAuto('', $VOClass, []);
    }

    /**
     * @url /skit/chasing/add
     * @auth skitChasingAdd
     * @req Srv.Apps.Proto.DTO.Skit.SkitChasingAddDTO
     * 追剧记录默认配置新增
     */
    public function skitChasingAdd(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($VOClass);
        if(!($DTOClass instanceof SkitChasingAddDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $setData        = $RouteConfData->getParamsResData($DTOClass);
        $skcfId         = SkitChasingCtl::tag()->skitChasingAdd($setData);
        $LogCtl         = LogCtl::tag();
        $logData        = $setData;
        if($skcfId < 1) {
            $errMsg     = '追剧记录默认配置新增失败';
            $LogCtl->logAdd(LogCtl::SKIT_CHASING_ADD, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $errMsg         = '追剧记录默认配置新增成功';
        $LogCtl->logAdd(LogCtl::SKIT_CHASING_ADD, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto('', null, []);
    }

    /**
     * @url /skit/chasing/edit
     * @auth skitChasingEdit
     * @req Srv.Apps.Proto.DTO.Skit.SkitChasingEditDTO
     * 追剧记录默认配置编辑
     */
    public function skitChasingEdit(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($VOClass);
        if(!($DTOClass instanceof SkitChasingEditDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $skcfId         = intval($DTOClass->getSkcfId());
        $setData        = $RouteConfData->getParamsResData($DTOClass);
        $LogCtl         = LogCtl::tag();
        $SkitChasingCtl = SkitChasingCtl::tag();
        $chasingSrc     = $SkitChasingCtl->getSkitChasingSrc($skcfId);
        if(!isset($chasingSrc['skcf_id'])){
            $errMsg     = '追剧记录默认配置不存在';
            $logData    = $setData;
            $LogCtl->logAdd(LogCtl::SKIT_CHASING_EDIT, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $result         = $SkitChasingCtl->skitChasingEdit($skcfId, $setData);
        $logData        = ['new' => $setData, 'old' => $chasingSrc];
        if(!$result){
            $errMsg     = '追剧记录默认配置编辑失败';
            $LogCtl->logAdd(LogCtl::SKIT_CHASING_EDIT, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $errMsg         = '追剧记录默认配置编辑成功';
        $LogCtl->logAdd(LogCtl::SKIT_CHASING_EDIT, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto('', null, []);
    }

    /**
     * @url /skit/chasing/del
     * @auth skitChasingDel
     * @req Srv.Apps.Proto.DTO.Skit.SkitChasingDelDTO
     * 追剧记录默认配置删除
     */
    public function skitChasingDel(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($VOClass);
        if(!($DTOClass instanceof SkitChasingDelDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $skcfId         = intval($DTOClass->getSkcfId());
        $setData        = $RouteConfData->getParamsResData($DTOClass);
        $LogCtl         = LogCtl::tag();
        $SkitChasingCtl = SkitChasingCtl::tag();
        $chasingSrc     = $SkitChasingCtl->getSkitChasingSrc($skcfId);
        $logData        = $setData;
        if(!isset($chasingSrc['skcf_id'])){
            $errMsg     = '追剧记录默认配置不存在';
            $LogCtl->logAdd(LogCtl::SKIT_CHASING_DEL, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $result         = SkitChasingCtl::tag()->skitChasingDel($skcfId);
        if(!$result){
            $errMsg     = '追剧记录默认配置删除失败';
            $LogCtl->logAdd(LogCtl::SKIT_CHASING_DEL, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $errMsg         = '追剧记录默认配置删除成功';
        $LogCtl->logAdd(LogCtl::SKIT_CHASING_DEL, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto('', null, []);
    }
}